﻿unit SysRoleUserService;

interface
{$I ../Config.inc}
uses
  FastMM4,
  FastMove,
  System.SysUtils,
  System.Classes,
  System.Types,
  Winapi.Windows,
  Data.DB,
  Data.Win.ADODB,
  Datasnap.DBClient,
  Datasnap.Provider,
  {$IFDEF UNICODE}
  AnsiStrings,
  {$ENDIF }
  QBParcel,
  DllSpread,
  Generics.Collections,
  DateUtils,
  QJson,
  UserConnection,
  DBAccessor,
  DllPlugin,
  StrUtils,
  SKSOFTComm,
  SysRoleUserModel,
  SysRoleUserBLL,
  SysRoleModel,
  SysUserModel;
  
type

  TSysRoleUserService = class(TSysRoleUserBLL)
  private

  protected

  public
  type  eRoleType=(rtNormalRole,rtManageRole,rtAll);

  {$IFDEF SERVER}
    function BeforeTreatment(const iItem:TSysRoleUser; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeTreatment(const iItems:TSysRoleUserList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function BeforeCheck(const iItem:TSysRoleUser; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeCheck(const iItems:TSysRoleUserList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function Check(const iItem:TSysRoleUser; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function Check(const iItems:TSysRoleUserList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function BeforeExecute(const iItem:TSysRoleUser; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeExecute(const iItems:TSysRoleUserList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function AfterTreatment(const iItem:TSysRoleUser; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function AfterTreatment(const iItems:TSysRoleUserList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
  {$ENDIF}

    function LoadByRoleId(const iRoleId:string; const iItems:TSysRoleUserList; out oError:string; const iJustUseUser:Boolean=False):Boolean;
    function LoadByUserId(const iUserId,iOrgId:string; const iRoleType:eRoleType; const iItems:TSysRoleUserList; out oError:string; const iJustUseRole:Boolean=False):Boolean;
  end;
  
implementation

{$IFDEF SERVER}
function TSysRoleUserService.BeforeTreatment(const iItem:TSysRoleUser; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysRoleUserService.BeforeTreatment(const iItems:TSysRoleUserList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysRoleUserService.BeforeCheck(const iItem:TSysRoleUser; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysRoleUserService.BeforeCheck(const iItems:TSysRoleUserList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysRoleUserService.Check(const iItem:TSysRoleUser; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysRoleUserService.Check(const iItems:TSysRoleUserList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysRoleUserService.BeforeExecute(const iItem:TSysRoleUser; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysRoleUserService.BeforeExecute(const iItems:TSysRoleUserList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysRoleUserService.AfterTreatment(const iItem:TSysRoleUser; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysRoleUserService.AfterTreatment(const iItems:TSysRoleUserList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;
{$ENDIF}

function TSysRoleUserService.LoadByRoleId(const iRoleId:string; const iItems:TSysRoleUserList; out oError:string; const iJustUseUser:Boolean=False):Boolean;
var
  Condition,OrderBy:string;
begin
  if (not SameStr(iRoleId,EmptyStr)) and (iItems<>nil) then
  begin
    if iJustUseUser then
    begin
      Condition:=Format(string('%s=''%s'' and %s=%d'),[TSysRoleUserModel.cRoleIdKey,iRoleId,TSysUserModel.cUserStatusKey,1]);
    end else begin
      Condition:=Format(string('%s=''%s'''),[TSysRoleUserModel.cRoleIdKey,iRoleId]);
    end;
    OrderBy:=TSysRoleUserModel.cUserIdKey+' ASC';

    if not LoadByCondition(iItems,Condition,oError,OrderBy,dblmView) then
    begin
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.LoadByRoleId方法失败！错误内容:传入的iRoleId或iItems为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSysRoleUserService.LoadByUserId(const iUserId,iOrgId:string; const iRoleType:eRoleType; const iItems:TSysRoleUserList; out oError:string; const iJustUseRole:Boolean=False):Boolean;
var
  Condition,OrderBy:string;
begin
  if (not SameStr(iUserId,EmptyStr))
    and (not SameStr(iOrgId,EmptyStr))
    and (iItems<>nil) then
  begin
    Condition:=Format(string('%s=''%s'' and %s=''%s'' '),
                                        [TSysRoleUserModel.cUserIdKey,iUserId,
                                        TSysRoleModel.cOrgIdKey,iOrgId]);
    case iRoleType of
    rtNormalRole:
      begin
        Condition:=Condition+Format(string(' and %s=0 '),[TSysRoleModel.cRoleTypeKey]);
      end;
    rtManageRole:
      begin
        Condition:=Condition+Format(string(' and %s=1 '),[TSysRoleModel.cRoleTypeKey]);
      end;
    end;
    if iJustUseRole then
    begin
      Condition:=Condition+Format(string(' and %s=1 '),[TSysRoleModel.cRoleStatusKey]);
    end;
    OrderBy:=TSysRoleUserModel.cRoleIdKey+' ASC';

    if not LoadByCondition(iItems,Condition,oError,OrderBy,eDBLoadMode.dblmView) then
    begin
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.LoadByUserId方法失败！错误内容:传入的iUserId或iOrgId或iItems为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;
  
end.
